package com.henry.magicsquare;

import com.henry.matrix.Matrix;

public class Square extends Matrix {

    public Square(int n, int[] data) {
        super(n, n, data);
    }

    public int diagonalSum() {
        int pos = 0;
        int sum = 0;
        for (int i = 0; i < this.getRows(); i++) {
            sum += this.data()[pos];
            pos = pos + this.getCols() + 1;
        }
        return sum;
    }

    public int backDiagonalSum() {
        int pos = this.getCols() - 1;
        int sum = 0;
        for (int i = 0; i < this.getRows(); i++) {
            sum += this.data()[pos];
            pos = pos + this.getCols() - 1;
        }
        return sum;
    }

    public int rowSum(int row) {
        int sum = 0;
        int pos = this.getCols() * row;
        for (int i = 0; i < this.getCols(); i++) {
            sum += this.data()[pos + i];
        }
        return sum;
    }

    public int colSum(int col) {
        int sum = 0;
        for (int i = 0; i < this.getRows(); i++) {
            sum += this.valueAt(i, col);
        }
        return sum;
    }
}
